package stringprocessing

object a {
  def main(args: Array[String]): Unit = {
    val logs = List(
      "2024-06-01|ERROR|spark job failed",
      "2024-06-01|INFO|spark job started",
      "2024-06-01|ERROR|executor lost",
      "2024-06-01|ERROR|spark job failed"
    )
    val errorMessages = logs.filter(_.split("\\|")(1) == "ERROR")
      .map(_.split("\\|")(2))

    val words = errorMessages.flatMap(_.split(" "))
      .map(_.toLowerCase)

    val wordCounts = words.groupBy(identity)
      .mapValues(_.size)

    val topWords = wordCounts.toList
      .sortBy(-_._2)
      .take(3)

    topWords.foreach { case (word, count) =>
      println(s"$word: $count")
    }
  }
}